#include "stdafx.h"
#include "CLine.h"


CLine::CLine(void)
{
}


CLine::CLine(CPoint pBegin, CPoint pEnd, COLORREF cPen)
{
	this->m_pBegin	= pBegin;
	this->m_pEnd	= pEnd;
	m_nAngle = NUM_OF_ANGLE;
	m_pCoordinates = new CPoint[NUM_OF_ANGLE];
	this->m_cPenColor = cPen;
	m_nPenStyle = PS_SOLID;
	m_nPenWidth = 2;
	m_nShapeType = LINE_T;
	m_bSelected = FALSE;
}

CLine::CLine (CPoint m_pBegin, CPoint m_pEnd, COLORREF m_clrBack, COLORREF m_cPenColor, BASESHAPE__T m_nShapeType, INT m_nRotate)
{
	this->m_nRotate = m_nRotate;
	this->m_pBegin = m_pBegin;
	this->m_pEnd = m_pEnd;
	this->m_clrBack = m_clrBack;
	this->m_cPenColor = m_cPenColor;
	this->m_nShapeType = m_nShapeType;
	m_brushColor.CreateSolidBrush(RGB(255,255,255));
	m_nAngle = NUM_OF_ANGLE;
	m_pCoordinates = new CPoint[NUM_OF_ANGLE];
	m_nPenWidth = 2;
	m_nPenStyle = PS_SOLID;
}


CLine::~CLine(void)
{
}

BOOL CLine::Draw(CDC *cDC)
{
	CPen cPen(m_nPenStyle, m_nPenWidth, m_cPenColor);
	CPen *pOldPen = cDC->SelectObject(&cPen);
	setCoordinate();
	cDC->MoveTo(m_pCoordinates[0]);
	cDC->LineTo(m_pCoordinates[1]);
	if(m_bSelected == TRUE)
		this->DrawTracker(cDC);
	cDC->SelectObject(pOldPen);
	pOldPen->DeleteObject();
	return FALSE;
}
void CLine::setCoordinate()
{
	m_pCoordinates[0]  = m_pBegin;
	m_pCoordinates[1]  = m_pEnd;
	m_pMax = m_pMin = m_pBegin;
	for(int i = 0; i < 2; i++)
	{
		m_pMin.x = min(m_pMin.x, m_pCoordinates[i].x);
		m_pMin.y = min(m_pMin.y, m_pCoordinates[i].y);
		m_pMax.x = max(m_pMax.x, m_pCoordinates[i].x);
		m_pMax.y = max(m_pMax.y, m_pCoordinates[i].y);

	}
}

void CLine::DrawTracker(CDC *cDC)
{
	const unsigned int Width = 4;
	CRect m_rectT(CPoint(m_pBegin.x - Width, m_pBegin.y - Width), CPoint(m_pBegin.x + Width, m_pBegin.y + Width));
	CPen cPen(m_nPenStyle, 1, m_cPenColor);
	cDC->SelectObject(&cPen);
	cDC->Rectangle(CRect(CPoint(m_pBegin.x - Width, m_pBegin.y - Width), CPoint(m_pBegin.x + Width, m_pBegin.y + Width)));
	cDC->Rectangle(CRect(CPoint(m_pEnd.x - Width, m_pEnd.y - Width), CPoint(m_pEnd.x + Width, m_pEnd.y + Width)));
}

void CLine::Serialize (CArchive &ar)
{
	if (ar.IsStoring() )
	{
		ar << m_pBegin << m_pEnd << m_clrBack << m_cPenColor << m_nShapeType << m_szText << m_color << m_nRotate;
	}
	else
	{
		int nShapeType;
		ar >> m_pBegin >> m_pEnd >> m_clrBack >> m_cPenColor >> nShapeType >> m_szText >> m_color >> m_nRotate;
		m_nShapeType = (BASESHAPE__T)nShapeType;
	}
}